layui.use(['form','layer'],function(){
	var form = layui.form,layer = layui.layer;
	
	
	$("#save-btn").on('click',function(){save();});
	initPara();
	function initPara(){
		var operateData = $("#operateData").val();
		var data = [];
		parent.$("input[name='operateData']").each(function(){
			var paraId = parent.$(this).val();
			if(!isBlank(paraId))
			data.push(paraId)
		});
		if(data.length == 0)
			return false;
		ajaxPost('../para/getOperCrData',{"paraIds":data.join(",")},
				function(data){
                    var para = data.data;
                    var content = [];
                    for(var i = 0 ;i < para.length;i++){
                    	if(!isBlank(operateData)&&operateData == para[i].id)
                    		content.push("<option value='"+para[i].id+"' selected>"+para[i].paraName+"</option>");
                    	else
                    		content.push("<option value='"+para[i].id+"'>"+para[i].paraName+"</option>");
                    }
					$("#sel>option:gt(1)").remove();
					$("#sel").append(content.join(""));
					form.render("select");
			   
				},
				function(data){
					parent.layer.alert(data.errorMsg, {
    					icon: 5,
    					title: "错误"
    					});
				});
	}
	
	function save(){
		var val = $("#sel").val();
		var newpara = $("#paraName").val();
		
		if(!isBlank(val) && !isBlank(newpara)){
			var text = $("#sel>option[value="+val+"]").text();
			layer.confirm('您既选择了已有数据['+text+']，又新增了数据['+newpara+']，请选择其一', {
				  btn: ['新增','已有'] //按钮
				}, function(){
				   addnew();
				}, function(){
				   addold();
				});
		}else if(!isBlank(val))
			addold();
		else if(!isBlank(newpara))
			addnew();
	}
	
	
	function addnew(){
		var newpara = $("#paraName").val();
		var reuseType = $("input[name='reuseType']:checked").val();
		var data = {'paraName':newpara,'reuseType':reuseType};
		ajaxPost('../para/saveCrPara',data,
					function(data){
						var index = $("#index").val();
						if(isBlank(index)){
							layer.alert("页面传值失败，请重新打开页面", {
								icon: 5,
								title: "错误"
								});
							return false;
						}
						var selectType = $("#selectType").val();
						if(selectType == 2){
							parent.$("#dg>tr:eq("+index+")").find("input[name='operateData']").val(data.data.id);
							parent.$("#dg>tr:eq("+index+")").find("input[name='data-input']").val(data.data.paraName);
						}else{
							parent.$("#dg>tr:eq("+index+")").find("input[name='outputData']").val(data.data.id);
							parent.$("#dg>tr:eq("+index+")").find("input[name='data-output']").val(data.data.paraName);
						}
						var idx = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
						parent.layer.close(idx);
			     
					},
					function(data){
						layer.alert(data.errorMsg, {
							icon: 5,
							title: "错误"
							});
						return false;
					});
	}
	
	function addold(){
		var id = $("#sel").val();
		var text = $("#sel>option[value="+id+"]").text();
		var index = $("#index").val();
		if(isBlank(index)){
			layer.alert("页面传值失败，请重新打开页面", {
				icon: 5,
				title: "错误"
				});
			return false;
		}
		parent.$("#dg>tr:eq("+index+")").find("input[name='operateData']").val(id);
		parent.$("#dg>tr:eq("+index+")").find("input[name='data-input']").val(text);
		var idx = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		parent.layer.close(idx);
	}
});